/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package test;

import java.awt.Insets;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.JTextField;

/**
 *
 * @author Frank
 */
public class Gui implements KeyListener {
    
    private JTextArea textarea;
    private JTextField textfield;
    private String lastLine = null;
    private VertaalClient vertaalClient;
    
    public Gui() {
        
        JFrame jframe = new JFrame();
        
        jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jframe.setTitle("Vertaalclient");
        jframe.setSize(800,450);
        jframe.setLocationRelativeTo(null);
        jframe.setLayout(null);
        
        // Componenten aanmaken
        this.textarea = new JTextArea();
        this.textfield = new JTextField();
        
        // Op juiste positie zetten
        this.textarea.setBounds(0, 0, 787, 380);
        this.textarea.setMargin(new Insets(10,10,10,10));
        this.textfield.setBounds(-1,380,787,35);
        this.textfield.setMargin(new Insets(10,10,10,10));
        
        // Toevoegen aan frame
        jframe.add(this.textarea);
        jframe.add(this.textfield);
        
        // Toetsen afvangen
        this.textfield.addKeyListener(this); 
        
        jframe.setVisible(true);
        
    }

    public void addVertaalClient(VertaalClient vertaalClient) {
        
        // De vertaalclient nesteld zichzelf in de gui om communicatie tussen console en netwerk mogelijk te maken
        this.vertaalClient = vertaalClient;
        
    }
    
    public void addConsole(String text) {
        this.textarea.setText(text + "\n" + this.textarea.getText());
    }
    
    public String getLastLine(){
        return this.lastLine;
    }
    
    @Override
    public void keyTyped(KeyEvent e) {
        
    }

    @Override
    public void keyPressed(KeyEvent e) {
        
        if(e.getKeyCode() == 10) { 
            
            // Tekst opvangen
            String text = this.textfield.getText();
            
            this.addConsole(text);
            
            // Input leegmaken
            this.textfield.setText("");
            
            // Laatst ingevoerde tekst onthouden
            this.vertaalClient.sendMessage(text);
            
        }
        
    }

    @Override
    public void keyReleased(KeyEvent e) {
       
    }
    
}
